package com.cn.springcloudwechat.controller;

import com.cn.springcloudwechat.common.ajax.AjaxRes;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.common.utils.base.Const;
import com.cn.springcloudwechat.controller.vaccine.log.LogInfo;
import com.cn.springcloudwechat.controller.vaccine.log.OperateType;
import com.cn.springcloudwechat.model.YmUser;
import com.cn.springcloudwechat.model.YmUserCollection;
import com.cn.springcloudwechat.service.YmUserCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/web/usercollectid/")
public class YmUserCollectionController {

    @Autowired
    private YmUserCollectionService ymUserCollectionService;

    @RequestMapping("course")
    @LogInfo(content = "收藏课程", operateType = OperateType.QUERY)
    public AjaxRes getCourse(AjaxRes ajaxRes){
        try{
             YmUser user= ThreadLocalUtil.getUser();
            Long userId=26l;
            List<Map<String,Object>> list=ymUserCollectionService.getCourse(user.getUserId());
            ajaxRes.setSucceed(list, Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    @RequestMapping("graphic")
    @LogInfo(content = "用户收藏的文章", operateType = OperateType.INSERT)
    public AjaxRes getGraphic(AjaxRes ajaxRes){
        try{
            YmUser user= ThreadLocalUtil.getUser();
            Long userId=26l;
            List<Map<String,Object>> list=ymUserCollectionService.getGraphic(user.getUserId());
            ajaxRes.setSucceed(list, Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    @RequestMapping("collection")
    @LogInfo(content = "收藏文章", operateType = OperateType.QUERY)
    public AjaxRes insertCollection(AjaxRes ajaxRes,String collectionType,Long collectionTypeId){
        try{
            YmUser user= ThreadLocalUtil.getUser();
            Long userId=26l;
            Integer integer2=ymUserCollectionService.getRepeat(collectionTypeId,user.getUserId());
            if(integer2 == 0){
                Integer integer=ymUserCollectionService.insertCollection(collectionType,collectionTypeId,user.getUserId());
                ajaxRes.setSucceed(integer,Const.SAVE_SUCCEED);
            }else {
                Integer integer1=ymUserCollectionService.deleteCollectionState(collectionTypeId,user.getUserId());
                ajaxRes.setSucceed(integer1,Const.DEL_SUCCEED);
            }
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
            return ajaxRes;
    }

    @RequestMapping("whetherCollection")
    @LogInfo(content = "是否已收藏文章", operateType = OperateType.QUERY)
    public AjaxRes whetherCollection(AjaxRes ajaxRes,Long collectionTypeId){
        try{
            YmUser user= ThreadLocalUtil.getUser();
            Long userId=26l;
            Integer integer2=ymUserCollectionService.getRepeat(collectionTypeId,user.getUserId());
            ajaxRes.setSucceed(integer2,Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }
}
